/**
 * Created by c on 2017-11-27.
 */

var flag="true";

$(document).ready(function () {
    $('#proNum').select2({
        placeholder: "请选择",
        language: "zh-CN"
    });

    //初始化下拉列表
    initSelect();

    //设置table背景颜色
    $("table#delTable").children().find("#back").css({"background-color":"#838383","color":"white"})

    //加载模态框
    $("div.dsdDia").dialog({
        title:"入库药品信息审填",
        btnValue:"确定",
        addBind:function(){
            var param = $("#dsdForm").serialize();
            $.post(ctx+"/drug/upDedels",param,function(flag){
                if(flag=="true"){
                    toastr.success("操作提示!","药品信息录入成功!");
                    $("div.dsdDia").dialog("close");
                    //页面添加信息
                    $("#delTable tr.dsdelsTr").each(function(){
                        if($(this).children("td.dsdrugNum").html()==$("#dsdForm input[name=drugNum]").val()){
                            $(this).children("td.dsdsdLotnum").html($("#dsdForm input[name=dsdLotnum]").val());
                            $(this).children("td.dsshengchan").html($("#dsdForm input[name=stoMfg]").val());
                            $(this).children("td.dsdsdValiddate").html($("#dsdForm input[name=dsdValiddate]").val());
                        }
                    });

                    $("#delTable tr.dsdelsTr").each(function(){
                        if($(this).children("td.dsdsdLotnum").html()==''||$(this).children("td.dsdsdLotnum").html()==null){
                            flag="false";
                        }
                    });

                    if(flag=="true"){
                        $("div.caoz").css({"display":"block"});
                    }
                }
            },"text");
        }
    });

    //初始化制单日期获取现在时间
    var mydate = new Date();
    var str = "" + mydate.getFullYear() + "-";
    str += (mydate.getMonth()+1) + "-";
    str += mydate.getDate();
    $("p.planDate").html(str);

    //初始化单据编号
    initdsNum();
});

$("button.ru").popover();

//初始化单据编号
function initdsNum(){
    $.post(ctx+"/drug/getDeNum",null,function(newNum){
        $("p.dsNum").html(newNum);
    },"text");
}

$(function () {
    $("#purDelsTable").hide();
    $('#purPlanTable').on('click-row.bs.table', function (e, row, element)
    {
        var param = "planNum="+row.planNum;
        if(flag=="true"){
            $("#purDelsTable").show();
        }
        $(".modal-body div #purDelsTable tr.delsTr").remove();
        $.post(ctx+"/drug/getPurDels",param,function(purdels){
            $(purdels).each(function(){
                var $td = "<tr class='delsTr'>"+
                    "<td class='drugName'>"+this.druginfo.drugName+"</td>"+
                    "<td class='drugStandard'>"+this.druginfo.drugStandard+"</td>"+
                    "<td class='delBuynum'>"+this.delBuynum+"</td>"+
                    "<td class='drugUnit'>"+this.druginfo.drugUnit+"</td>"+
                    "<td class='drugPreparation'>"+this.druginfo.drugPreparation+"</td>"+
                    "<td class='delOneprice'>"+this.delOneprice+"</td>"+
                "</tr>";
                $($td).appendTo("#purDelsTable");
            });
        },"json");
    });
});

var flag="true";

//禁止显示明细信息
function colseDels(btn){
    if($(btn).attr("id")=='closedels'){
        $(btn).html("允许显示明细");
        $(btn).attr("id","keyi");
        $("#purDelsTable").hide();
        flag="false"
        return
    }
    if($(btn).attr("id")=='keyi'){
        $(btn).html("禁止显示明细");
        $(btn).attr("id","closedels");
        $("#purDelsTable").show();
        flag="true";
        return;
    }
}

function addpur(){
    var arr = $("#purPlanTable").bootstrapTable("getSelections");
    $("input[name=planNum]").val(arr[0].planNum);
    $(".gouplanDate").html(arr[0].planDate);
    $("div#choosePlan .modal").modal("hide");
    $("div#choosePlan").css({"display":"none"});
    var param = "planNum="+arr[0].planNum;

    $.post(ctx+"/drug/toMap",param,function(flag){
        if(flag=="true"){
            //加载入库明细信息
            $.post(ctx+"/drug/getPurDels",param,function(purdels){
                $("#delTable tr.dsdelsTr").remove();
                $(purdels).each(function(){
                    var $td = "<tr class='dsdelsTr'>"+
                        "<td class='dsdrugNum'>"+this.druginfo.drugNum+"</td>"+
                        "<td class='dsdrugName'>"+this.druginfo.drugName+"</td>"+
                        "<td class='dsdrugStandard'>"+this.druginfo.drugStandard+"</td>"+
                        "<td class='dsdelBuynum'>"+this.delBuynum+"</td>"+
                        "<td class='dsdelRetailprice'>"+this.delRetailprice+"</td>"+
                        "<td class='dsdsdLotnum'></td>"+
                        "<td class='dsshengchan'></td>"+
                        "<td class='dsdsdValiddate'></td>"+
                        "<td>"+
                        '<button class="layui-btn layui-btn-normal layui-btn-mini updateDrug" type="button" onclick="dsdEdit(this)">编辑</button>' +
                        "</td>"+
                        "</tr>";
                    $($td).appendTo("#delTable");
                    $("div.caoz").css({"display":"none"});
                });
            },"json");
        }
    },"text");
}

//搜索日期插件
$(function () {
    laydate.render({
        elem:'[name=stoMfg]'
    })
    laydate.render({
        elem:'[name=dsdValiddate]'
    })
});

function dsdEdit(td){
    $("div.dsdDia").css({"display":"block"});
    $("div.dsdDia").dialog("open");
    var param = "drugNum="+$(td).parent().parent().children(":eq(0)").html();
    $.post(ctx+"/drug/editdedels",param,function(dsd){
       $("form#dsdForm input[name=drugNum]").val(dsd.drugNum);
        $("form#dsdForm input[name=drugName]").val($(td).parent().parent().children(":eq(1)").html());
        $("form#dsdForm input[name=dsdLotnum]").val(dsd.dsdLotnum);
        $("form#dsdForm input[name=stoMfg]").val(dsd.stoMfg);
        $("form#dsdForm input[name=dsdValiddate]").val(dsd.dsdValiddate);
    },"json");
}

//初始化下拉列表
function initSelect(){
    $("#proNum").children(":gt(0)").remove();
    $.post(ctx+"/pur/getSelect",null,function(map){
        $(map.pro).each(function(){
            var $opt = $("<option/>");
            $opt.val(this.proNum);
            $opt.html(this.proName);
            $opt.appendTo("#proNum");
        });
    },"json");
}

//用户选择供应商绑定供应商信息
function selChange(sel){
    var param = "proNum="+$(sel).val();
    $.post(ctx+"/drug/getPro",param,function(pro){

        $("p#proPerson").html(pro.proPerson);
        $("p#prophone").html(pro.proPhone);
    },"json");
}

//绑定采购单
function bindPlan(){
    $("div#choosePlan").css({"display":"block"});
    $("#purPlanTable").bootstrapTable("refresh");
    $("table#purDelsTable tr.delsTr").remove();
}

//初始化订单状态
function initState(state){
    return state==1?'<span style="color: green;">已通过</span>':'';
}

function initruState(state){
    return state==0?'<span>未入库</span>':'';
}

//入库
function addDrugsto(){
    var proNum = $("select[name=proNum]").val();
    var planNum = $("input[name=planNum]").val();
    var dsNum = $("p.dsNum").html();
    var planDate = $("p.planDate").html();
    var dsVerifier = $("p.dsVerifier").html();

    var param = "proNum="+proNum+"&planNum="+planNum+"&dsNum="+dsNum+"&planDate="+planDate+"&dsVerifier="+dsVerifier;
    $.post(ctx+"/drug/ruku",param,function(flag){
        if(flag=="true"){
            swal({
                title: '<h1 style="font-weight:bold;color:red">出库操作成功!</h1>',
                type: 'success',
                html: '<a href= "'+ctx+'/stock/stoView/index" class="btn btn-primary">查看库存信息</a>&nbsp;&nbsp;&nbsp;&nbsp;' +
                '<a href= "'+ctx+'/drug/addlog/index?dsNum='+dsNum+'" class="btn btn-info">查看入库记录</a>',
                showCloseButton: true,
                confirmButtonColor: 'gray',
                confirmButtonText: ' <i class="mui-icon mui-icon-refresh"></i>取消'
            }).then(function(){
                location.href = ctx+"/drug/add/index";
            });
        }else{
            toastr.error("操作提示","系统繁忙,入库失败!")
        }
    },"text");
}

